package com.gxa.insurance.service.impl;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.RepairDepotMapper;
import com.gxa.insurance.pojo.RepairDepot;
import com.gxa.insurance.service.RepairDepotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wang li sha
 * @date 2022-06-06 16:33
 */
@Service("RepairDepotService")
@Transactional(rollbackFor = Throwable.class)
public class RepairDepotServiceImpl implements RepairDepotService {
    @Autowired
    RepairDepotMapper repairDepotMapper;

    @Override
    public Integer insert(RepairDepot repairDepot) throws Exception {
        Integer insert = repairDepotMapper.insert(repairDepot);
        if (insert != 1) {
            throw new BusinessException(ExceptionCode.INSERT_REPAIRDEPOT_FAILED);
        }
        return insert;
    }

    @Override
    public Integer update(RepairDepot repairDepot) throws Exception {
        Integer update = repairDepotMapper.update(repairDepot);
        if (update != 1) {
            throw new BusinessException(ExceptionCode.UPDATE_REPAIRDEPOT_FAILED);
        }
        return update;
    }

    @Override
    public Integer deleteById(Integer id) throws Exception {
        Integer delete = repairDepotMapper.deleteById(id);
        if (delete != 1) {
            throw new BusinessException(ExceptionCode.DELETE_REPAIRDEPOT_FAILED);
        }
        return delete;
    }

    @Override
    public RepairDepot selectById(Integer id) throws Exception {
        RepairDepot repairDepot = repairDepotMapper.selectById(id);
        if (repairDepot == null) {
            throw new BusinessException(ExceptionCode.REPAIRDEPOT_NOT_FONUD);
        }
        return repairDepot;
    }

    @Override
    public List<RepairDepot> selectAll() throws Exception {
        List<RepairDepot> repairDepots = repairDepotMapper.selectAll();
        if (repairDepots.size() == 0) {
            throw new BusinessException(ExceptionCode.REPAIRDEPOT_IS_EMPTY);
        }
        return repairDepots;
    }


    @Override
    public List<RepairDepot> selectAllByPage(Integer page, Integer size) throws Exception {
        List<RepairDepot> repairDepots = repairDepotMapper.selectAllByPage((page - 1) * size, size);
        if (repairDepots.size() == 0) {
            throw new BusinessException(ExceptionCode.REPAIRDEPOT_IS_EMPTY);
        }
        return repairDepots;
    }

    @Override
    public List<RepairDepot> selectLikeName(String name) throws Exception {
        List<RepairDepot> repairDepots = repairDepotMapper.selectLikeName(name);
        if (repairDepots.size() == 0) {
            throw new BusinessException(ExceptionCode.REPAIRDEPOT_IS_EMPTY);
        }
        return repairDepots;
    }

    @Override
    public List<RepairDepot> selectLikeNameByPage(Integer page, Integer size, String name) throws Exception {
        List<RepairDepot> repairDepots = repairDepotMapper.selectLikeNameByPage((page - 1) * size, size, name);
        if (repairDepots.size() == 0) {
            throw new BusinessException(ExceptionCode.REPAIRDEPOT_IS_EMPTY);
        }
        return repairDepots;
    }
}

